{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=requests.Session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {'accept-language': 'zh-CN,zh;q=0.9',\n",
    "            'Host': 'www.zhihu.com',\n",
    "            'Upgrade-Insecure-Requests': '1',\n",
    "            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'\n",
    "            }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "question_id = 405043851\n",
    "answers_id = 1322220479\n",
    "limit = 20\n",
    "offset = 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "answers_url_open = \"https://www.zhihu.com/api/v4/questions/{}/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={}&offset={}&platform=desktop&sort_by=default\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "answers_url_collapsed = \"https://www.zhihu.com/api/v4/questions/{}/collapsed-answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={}&offset={}&sort_by=defaul\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\program files\\my_work\\venv\\lib\\site-packages\\urllib3\\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "answers_response = s.get(url=answers_url_collapsed.format(question_id,limit,offset),headers=headers,verify=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "comments_url_open = \"https://www.zhihu.com/api/v4/answers/{}/comments?limit={}&offset={}&order=reverse&status=open\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "comments_url_collapsed = \"https://www.zhihu.com/api/v4/answers/{}/comments?limit={}&offset={}&order=reverse&status=collapsed\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\program files\\my_work\\venv\\lib\\site-packages\\urllib3\\connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "comments_response = s.get(url=comments_url_collapsed.format(answers_id,limit,offset),headers=headers,verify=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "comments_response_json=comments_response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['paging', 'data'])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments_response_json.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'is_end': False,\n",
       " 'is_start': True,\n",
       " 'next': 'https://www.zhihu.com/api/v4/answers/1322220479/comments?limit=20&offset=20&order=reverse&status=collapsed',\n",
       " 'previous': 'https://www.zhihu.com/api/v4/answers/1322220479/comments?limit=20&offset=0&order=reverse&status=collapsed',\n",
       " 'totals': 26}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments_response_json['paging']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 988988380,\n",
       " 'type': 'comment',\n",
       " 'url': 'https://www.zhihu.com/api/v4/comments/988988380',\n",
       " 'content': '刚需个屁，房子够30亿人住。',\n",
       " 'featured': False,\n",
       " 'top': False,\n",
       " 'collapsed': True,\n",
       " 'is_author': False,\n",
       " 'is_delete': False,\n",
       " 'created_time': 1594048377,\n",
       " 'resource_type': 'answer',\n",
       " 'reviewing': False,\n",
       " 'allow_like': True,\n",
       " 'allow_delete': False,\n",
       " 'allow_reply': True,\n",
       " 'allow_vote': True,\n",
       " 'can_recommend': False,\n",
       " 'can_collapse': False,\n",
       " 'attached_info': 'ggIGCAwQARgA',\n",
       " 'author': {'role': 'normal',\n",
       "  'member': {'id': '6a1a7d73afe52b456c26eb75ca3f7dfb',\n",
       "   'url_token': 'e-e-slow',\n",
       "   'name': '哦哦slow',\n",
       "   'avatar_url': 'https://pic4.zhimg.com/v2-12fb567366f4dfec2b6424888b886211.jpg?source=06d4cd63',\n",
       "   'avatar_url_template': 'https://pic4.zhimg.com/v2-12fb567366f4dfec2b6424888b886211.jpg?source=06d4cd63',\n",
       "   'is_org': False,\n",
       "   'type': 'people',\n",
       "   'url': 'https://www.zhihu.com/api/v4/people/6a1a7d73afe52b456c26eb75ca3f7dfb',\n",
       "   'user_type': 'people',\n",
       "   'headline': '',\n",
       "   'badge': [],\n",
       "   'gender': -1,\n",
       "   'is_advertiser': False}},\n",
       " 'is_parent_author': False,\n",
       " 'vote_count': 4,\n",
       " 'reply_to_author': {'role': 'normal',\n",
       "  'member': {'id': '859d823f0f7a689484b2b865449cab42',\n",
       "   'url_token': 'te-shu-fu-hao-4-20',\n",
       "   'name': '特殊符号',\n",
       "   'avatar_url': 'https://pic4.zhimg.com/da8e974dc.jpg?source=06d4cd63',\n",
       "   'avatar_url_template': 'https://pic4.zhimg.com/da8e974dc.jpg?source=06d4cd63',\n",
       "   'is_org': False,\n",
       "   'type': 'people',\n",
       "   'url': 'https://www.zhihu.com/api/v4/people/859d823f0f7a689484b2b865449cab42',\n",
       "   'user_type': 'people',\n",
       "   'headline': '',\n",
       "   'badge': [],\n",
       "   'gender': -1,\n",
       "   'is_advertiser': False}},\n",
       " 'voting': False,\n",
       " 'disliked': False}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments_response_json['data'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'刚需个屁，房子够30亿人住。'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "comments_response_json['data'][0]['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['刚需个屁，房子够30亿人住。',\n",
       " '这张口就来的优越感，不知道的还以为是哪个地主大老爷穿越了呢',\n",
       " '孩子一落地，四脚吞金兽，可笑又无奈！',\n",
       " '<p>哎，这个年代啃老都不丢人了。</p>',\n",
       " '太恶心了，越穷越生',\n",
       " '哦，真是谢谢你了，真有脸，但是关你屁事呢',\n",
       " '纯纯的扯淡 就是当时的借口',\n",
       " '尼玛那你这种人活该孤独终老',\n",
       " '房价真他妈的该死',\n",
       " '倒数第三段就是胡说八道，那就是根结所在',\n",
       " '<p>女生怕什么？只有男的才有可能真的没资格结婚生子</p>',\n",
       " '现在的中国谈孝顺简直可笑',\n",
       " '口气真大，虽然我在吐槽，但真指不定比你混得好！我和你说父母不应该过分的把自己未达成的愿望强压在孩子身上，你和我扯我值不值得被父母期待？有时间多读书，少逛知乎',\n",
       " '少在这里杠了，看了下你的评论，一百多回答才这么点赞，杠都杠不到点上',\n",
       " '<p>活该</p>',\n",
       " '<p>你肯定疯了，这不相当于房价降80%了！</p>',\n",
       " '<p>借个楼，楼主是个偏执狂，渣男或者渣女，非处，鼓吹婚前同居，他发私信给别人第一句话说，十分赞同你的观点，第二句话就说你有病，建议就医，并且把对方拉黑，有图有真相。像楼主这样动不动骂人拉黑别人的，素质可见一斑，应该没上过大学。</p>',\n",
       " '以前的我：哎，我真是个废物<br>现在的我：爷就是一个废物✌️',\n",
       " '什么都不要就通情达理善解人意 真是有够恶心的呢',\n",
       " '你是不是统治阶级，不是就别这样冠冕堂皇说话，那你去西部支教吧，装逼也分地方的']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i['content'] for i in comments_response_json['data']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "answers_response_json=answers_response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'is_end': False,\n",
       " 'totals': 290,\n",
       " 'previous': 'http://www.zhihu.com/api/v4/questions/405043851/collapsed-answers?sort_by=defaul&include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics%3Bsettings.table_of_content.enabled%3B&limit=20&offset=0',\n",
       " 'is_start': True,\n",
       " 'next': 'http://www.zhihu.com/api/v4/questions/405043851/collapsed-answers?sort_by=defaul&include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics%3Bsettings.table_of_content.enabled%3B&limit=20&offset=20'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answers_response_json['paging']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'relevant_info': {'relevant_type': '',\n",
       "  'is_relevant': False,\n",
       "  'relevant_text': ''},\n",
       " 'suggest_edit': {'status': True,\n",
       "  'reason': '回答被建议修改：\\n违反知乎社区管理规定',\n",
       "  'title': '为什么回答会被建议修改',\n",
       "  'url': 'zhihu://questions/24752645',\n",
       "  'unnormal_details': {'status': 'suggest',\n",
       "   'note': '',\n",
       "   'reason': '违反知乎社区管理规定',\n",
       "   'description': '回答建议修改',\n",
       "   'reason_id': 35011},\n",
       "  'tip': '作者修改内容通过后，回答会重新显示恢复正常'},\n",
       " 'relationship': {'is_authorized': False,\n",
       "  'is_author': False,\n",
       "  'is_thanked': False,\n",
       "  'is_nothelp': False,\n",
       "  'voting': 0},\n",
       " 'editable_content': '',\n",
       " 'mark_infos': [],\n",
       " 'excerpt': '回答建议修改',\n",
       " 'annotation_action': [],\n",
       " 'admin_closed_comment': False,\n",
       " 'collapsed_by': 'community',\n",
       " 'can_comment': {'status': True, 'reason': ''},\n",
       " 'created_time': 1605135742,\n",
       " 'id': 1570652846,\n",
       " 'voteup_count': 0,\n",
       " 'collapse_reason': '',\n",
       " 'is_collapsed': True,\n",
       " 'is_sticky': False,\n",
       " 'url': 'http://www.zhihu.com/api/v4/answers/1570652846',\n",
       " 'comment_permission': 'all',\n",
       " 'author': {'avatar_url_template': 'https://pic2.zhimg.com/da8e974dc.jpg?source=c8b7c179',\n",
       "  'badge': [],\n",
       "  'name': '152',\n",
       "  'url': 'http://www.zhihu.com/api/v4/people/aa52887c23dd816694ad051575c094eb',\n",
       "  'gender': -1,\n",
       "  'user_type': 'people',\n",
       "  'url_token': '152-28',\n",
       "  'is_advertiser': False,\n",
       "  'avatar_url': 'https://pic4.zhimg.com/da8e974dc_l.jpg?source=c8b7c179',\n",
       "  'is_org': False,\n",
       "  'headline': '',\n",
       "  'follower_count': 4,\n",
       "  'type': 'people',\n",
       "  'id': 'aa52887c23dd816694ad051575c094eb'},\n",
       " 'question': {'question_type': 'normal',\n",
       "  'title': '00后比90后少了4700万，大家对生孩子为什么越来越抗拒？',\n",
       "  'url': 'http://www.zhihu.com/api/v4/questions/405043851',\n",
       "  'created': 1593874604,\n",
       "  'type': 'question',\n",
       "  'id': 405043851,\n",
       "  'updated_time': 1593874604},\n",
       " 'updated_time': 1605135742,\n",
       " 'content': '',\n",
       " 'comment_count': 0,\n",
       " 'extras': '',\n",
       " 'reshipment_settings': 'allowed',\n",
       " 'answer_type': 'answer',\n",
       " 'reward_info': {'reward_member_count': 0,\n",
       "  'tagline': '',\n",
       "  'reward_total_money': 0,\n",
       "  'can_open_reward': False,\n",
       "  'is_rewardable': False},\n",
       " 'is_copyable': True,\n",
       " 'type': 'answer',\n",
       " 'thumbnail': '',\n",
       " 'is_normal': False}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answers_response_json['data'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answers_response_json['data'][0]['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['',\n",
       " '<p>养不起。</p>',\n",
       " '<p>衣食住行教育医疗</p>',\n",
       " '<p>我也不知道 </p>',\n",
       " '<p>自己还是孩子</p>',\n",
       " '<p>韭菜</p>',\n",
       " '<p>城市化</p>',\n",
       " '<p>不一定。</p>',\n",
       " '',\n",
       " '<p>养不起呗</p>',\n",
       " '<p>因为随着男孩子越来越没担当，生孩子对女的来说越来越吃亏，毕竟孩子得女的生。</p><p>再者，就我个人来说，我并没有准备好承担一个人的未来，也没准备好贡献我未来30年给它。</p>',\n",
       " '<p>没钱</p>',\n",
       " '<p>因为穷</p>',\n",
       " '<p>多好的事啊。</p>',\n",
       " '<p>我想知道10后比00后少多少</p>',\n",
       " '<p>题主的数学和历史不会都是体育老师教的吧？这不就是当年那批独生子女到了生育年龄了吗。</p>',\n",
       " '',\n",
       " '<p>当然要怪资本家</p>',\n",
       " '<p>养是可以，谁带呢？</p><p></p>',\n",
       " '<p>那你得问计生委</p>']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i['content'] for i in answers_response_json['data']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import time\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {'accept-language': 'zh-CN,zh;q=0.9',\n",
    "            'Host': 'www.zhihu.com',\n",
    "            'Upgrade-Insecure-Requests': '1',\n",
    "            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'\n",
    "            }\n",
    "\n",
    "question_id = 405043851\n",
    "answers_id = 1322220479\n",
    "limit = 20\n",
    "offset = 0\n",
    "\n",
    "answers_url_open = \"https://www.zhihu.com/api/v4/questions/{}/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={}&offset={}&platform=desktop&sort_by=default\"\n",
    "answers_url_collapsed = \"https://www.zhihu.com/api/v4/questions/{}/collapsed-answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={}&offset={}&sort_by=defaul\"\n",
    "\n",
    "comments_url_open = \"https://www.zhihu.com/api/v4/answers/{}/comments?limit={}&offset={}&order=reverse&status=open\"\n",
    "comments_url_collapsed = \"https://www.zhihu.com/api/v4/answers/{}/comments?limit={}&offset={}&order=reverse&status=collapsed\"\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_all_comments(answers_id = 15108995 ,limit = 20,offset=0):\n",
    "    s=requests.Session()\n",
    "    comments=[]\n",
    "    comments_url_open = \"https://www.zhihu.com/api/v4/answers/{0}/comments?limit={1}&offset={2}&order=reverse&status=open\"\n",
    "    comments_url_collapsed = \"https://www.zhihu.com/api/v4/answers/{0}/comments?limit={1}&offset={2}&order=reverse&status=collapsed\"\n",
    "    comments_totals=0\n",
    "    comments_get=0\n",
    "    start_time=time.time()\n",
    "\n",
    "    url=comments_url_open.format(answers_id,limit,offset)\n",
    "    while(True):\n",
    "        comments_response = s.get(url=url,headers=headers,)#verify=False)\n",
    "        comments_response_json=comments_response.json()\n",
    "        \n",
    "        comments.extend(comments_response_json['data'])\n",
    "        url=comments_response_json['paging']['next']\n",
    "        comments_totals=comments_response_json['paging']['totals']\n",
    "        comments_get+=len(comments_response_json['data'])\n",
    "        \n",
    "        if comments_response_json['paging']['is_end'] or comments_totals == 0 or len(comments_response_json['data']) == 0:\n",
    "            print(\"|  |----comments:open      answers_id:{0},总共:{1},耗时:{2}s\".format(answers_id,comments_totals,time.time()-start_time))\n",
    "            break\n",
    "        print(\"|  |----comments:open      总共:{0},进度:{1}%,已耗时:{2}s\".format(comments_totals,round((comments_get) * 100 / comments_totals),time.time()-start_time), end=\"\\r\")\n",
    "        time.sleep(1)\n",
    "    \n",
    "    comments_totals=0\n",
    "    comments_get=0\n",
    "    start_time=time.time()\n",
    "    \n",
    "    url=comments_url_collapsed.format(answers_id,limit,offset)\n",
    "    while(True):\n",
    "        comments_response = s.get(url=url,headers=headers,)#verify=False)\n",
    "        comments_response_json=comments_response.json()\n",
    "        \n",
    "        comments.extend(comments_response_json['data'])\n",
    "        url=comments_response_json['paging']['next']\n",
    "        comments_totals=comments_response_json['paging']['totals']\n",
    "        comments_get+=len(comments_response_json['data'])\n",
    "        \n",
    "        if comments_response_json['paging']['is_end'] or comments_totals == 0 or len(comments_response_json['data']) == 0:\n",
    "            print(\"|  |----comments:collapsed answers_id:{0},总共:{1},耗时:{2}s\".format(answers_id,comments_totals,time.time()-start_time))\n",
    "            break\n",
    "        print(\"|  |----comments:collapsed 总共:{0},进度:{1}%，已耗时:{2}s\".format(comments_totals,round((comments_get) * 100 / comments_totals),time.time()-start_time), end=\"\\r\")\n",
    "        time.sleep(1)\n",
    "        \n",
    "    return comments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_all_answers(question_id = 20427672 ,limit = 20,offset=0):\n",
    "    s=requests.Session()\n",
    "    answers=[]\n",
    "    answers_url_open = \"https://www.zhihu.com/api/v4/questions/{0}/answers?include=data%5B%2A%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B%2A%5D.mark_infos%5B%2A%5D.url%3Bdata%5B%2A%5D.author.follower_count%2Cbadge%5B%2A%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={1}&offset={2}&platform=desktop&sort_by=default\"\n",
    "    answers_url_collapsed = \"https://www.zhihu.com/api/v4/questions/{0}/collapsed-answers?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%2Cis_labeled%2Cis_recognized%2Cpaid_info%2Cpaid_info_content%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cbadge%5B*%5D.topics%3Bsettings.table_of_content.enabled%3B&limit={1}&offset={2}&sort_by=defaul\"\n",
    "    answers_totals=0\n",
    "    answers_get=0\n",
    "    start_time=time.time()\n",
    "    \n",
    "    url=answers_url_open.format(question_id,limit,offset)\n",
    "    while(True):\n",
    "        answers_response = s.get(url=url,headers=headers,)#verify=False)\n",
    "        answers_response_json=answers_response.json()\n",
    "        \n",
    "        for answer in answers_response_json['data']:\n",
    "            answer['comments'] = get_all_comments(answer['id'])\n",
    "        answers.extend(answers_response_json['data'])\n",
    "        \n",
    "        url=answers_response_json['paging']['next']\n",
    "        answers_totals=answers_response_json['paging']['totals']\n",
    "        answers_get+=len(answers_response_json['data'])\n",
    "        \n",
    "        if answers_response_json['paging']['is_end'] or answers_totals==0 or len(answers_response_json['data'])==0:\n",
    "            print(\"|--answers:open      question_id:{0},总共:{1},耗时:{2}s\".format(question_id,answers_totals,time.time()-start_time))\n",
    "            break\n",
    "        print(\"|--answers:open      总共:{0},进度:{1}%，已耗时:{2}s\".format(answers_totals,round((answers_get) * 100 / answers_totals),time.time()-start_time), end=\"\\r\")\n",
    "        time.sleep(1)\n",
    "\n",
    "    answers_totals=0\n",
    "    answers_get=0\n",
    "    start_time=time.time()\n",
    "        \n",
    "    url=answers_url_collapsed.format(question_id,limit,offset)\n",
    "    while(True):\n",
    "        answers_response = s.get(url=url,headers=headers,)#verify=False)\n",
    "        answers_response_json=answers_response.json()\n",
    "        \n",
    "        for answer in answers_response_json['data']:\n",
    "            answer['comments'] = get_all_comments(answer['id'])\n",
    "        answers.extend(answers_response_json['data'])\n",
    "        \n",
    "        url=answers_response_json['paging']['next']\n",
    "        answers_totals=answers_response_json['paging']['totals']\n",
    "        answers_get+=len(answers_response_json['data'])\n",
    "        \n",
    "        if answers_response_json['paging']['is_end'] or answers_totals==0 or len(answers_response_json['data'])==0:\n",
    "            print(\"|--answers:collapsed question_id:{0},总共:{1},耗时:{2}s\".format(question_id,answers_totals,time.time()-start_time))\n",
    "            break\n",
    "        print(\"|--answers:collapsed 总共:{0},进度:{1}%，已耗时:{2}s\".format(answers_totals,round((answers_get) * 100 / answers_totals),time.time()-start_time), end=\"\\r\")\n",
    "        time.sleep(1)\n",
    "    \n",
    "    return answers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|  |----comments:open      answers_id:15108995,总共:31,耗时:4.450554609298706s\n",
      "|  |----comments:collapsed answers_id:15108995,总共:0,耗时:0.9159383773803711s\n"
     ]
    }
   ],
   "source": [
    "comments = get_all_comments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|  |----comments:open      answers_id:15108995,总共:31,耗时:5.054594993591309s\n",
      "|  |----comments:collapsed answers_id:15108995,总共:0,耗时:1.0051689147949219s\n",
      "|  |----comments:open      answers_id:15112441,总共:0,耗时:2.2449188232421875s\n",
      "|  |----comments:collapsed answers_id:15112441,总共:0,耗时:1.009490728378296s\n",
      "|  |----comments:open      answers_id:15120916,总共:0,耗时:2.6723241806030273s\n",
      "|  |----comments:collapsed answers_id:15120916,总共:0,耗时:0.9770753383636475s\n",
      "|  |----comments:open      answers_id:87429339,总共:0,耗时:2.7106990814208984s\n",
      "|  |----comments:collapsed answers_id:87429339,总共:0,耗时:1.0345191955566406s\n",
      "|  |----comments:open      answers_id:145021131,总共:0,耗时:2.9803831577301025s\n",
      "|  |----comments:collapsed answers_id:145021131,总共:0,耗时:0.9823293685913086s\n",
      "|  |----comments:open      answers_id:72701937,总共:0,耗时:2.0535073280334473s\n",
      "|  |----comments:collapsed answers_id:72701937,总共:0,耗时:1.0638508796691895s\n",
      "|  |----comments:open      answers_id:433749612,总共:0,耗时:2.5707969665527344s\n",
      "|  |----comments:collapsed answers_id:433749612,总共:0,耗时:1.0694575309753418s\n",
      "|  |----comments:open      answers_id:433598742,总共:0,耗时:2.3955883979797363s\n",
      "|  |----comments:collapsed answers_id:433598742,总共:0,耗时:0.882256269454956s\n",
      "|  |----comments:open      answers_id:111971184,总共:1,耗时:2.3350155353546143s\n",
      "|  |----comments:collapsed answers_id:111971184,总共:0,耗时:1.2771577835083008s\n",
      "|  |----comments:open      answers_id:433014604,总共:2,耗时:2.43444561958313s\n",
      "|  |----comments:collapsed answers_id:433014604,总共:0,耗时:0.9446620941162109s\n",
      "|  |----comments:open      answers_id:144281377,总共:0,耗时:2.210969924926758s\n",
      "|  |----comments:collapsed answers_id:144281377,总共:0,耗时:0.8994660377502441s\n",
      "|  |----comments:open      answers_id:141683668,总共:0,耗时:2.402691125869751s\n",
      "|  |----comments:collapsed answers_id:141683668,总共:0,耗时:0.9574799537658691s\n",
      "|--answers:open      question_id:20427672,总共:12,耗时:46.88902688026428s\n",
      "|  |----comments:open      answers_id:15109970,总共:0,耗时:1.9936411380767822s\n",
      "|  |----comments:collapsed answers_id:15109970,总共:0,耗时:0.8940403461456299s\n",
      "|  |----comments:open      answers_id:15108626,总共:5,耗时:2.3719522953033447s\n",
      "|  |----comments:collapsed answers_id:15108626,总共:0,耗时:0.9353146553039551s\n",
      "|  |----comments:open      answers_id:15108250,总共:0,耗时:2.3082568645477295s\n",
      "|  |----comments:collapsed answers_id:15108250,总共:0,耗时:0.8915913105010986s\n",
      "|  |----comments:open      answers_id:15108148,总共:5,耗时:2.0906198024749756s\n",
      "|  |----comments:collapsed answers_id:15108148,总共:0,耗时:0.9262351989746094s\n",
      "|--answers:collapsed question_id:20427672,总共:4,耗时:13.682799816131592s\n"
     ]
    }
   ],
   "source": [
    "answers = get_all_answers()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(answers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('20427672.json','w') as f:\n",
    "    json.dump(answers,f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nTraceback (most recent call last):\\n  File \"zhihu_requests.py\", line 111, in <module>\\n    answers = get_all_answers(405043851)\\n  File \"zhihu_requests.py\", line 73, in get_all_answers\\n    answer[\\'comments\\'] = get_all_comments(answer[\\'id\\'])\\n  File \"zhihu_requests.py\", line 45, in get_all_comments\\n    comments.extend(comments_response_json[\\'data\\'])\\nKeyError: \\'data\\'\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Traceback (most recent call last):\n",
    "  File \"zhihu_requests.py\", line 111, in <module>\n",
    "    answers = get_all_answers(405043851)\n",
    "  File \"zhihu_requests.py\", line 73, in get_all_answers\n",
    "    answer['comments'] = get_all_comments(answer['id'])\n",
    "  File \"zhihu_requests.py\", line 45, in get_all_comments\n",
    "    comments.extend(comments_response_json['data'])\n",
    "KeyError: 'data'\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{' Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a': '1605709502',\n",
       " ' __a': '47391232.1605706630..1605706630.11.1.11.11',\n",
       " ' __c': '1605706630',\n",
       " ' __g': '-',\n",
       " ' __l': 'l',\n",
       " ' __zp_stoken__': '9422bPBgWOWlsOmclKXl5e01fQjh8clNjY0EMZXEhHEA5XBdNYxJoMlVYSgUKR1FhFx9lUXBnXDsJRHtXfT8%2BBh5yBRIMdzZ4TF1IJjcgMlRlShkcQwQ9PR94c0hDCwZbEU5vQjUGLUR2PEs0YQ%3D%3D',\n",
       " 'Hm_lvt_194df3105ad7148dcf2b98a91b5e727a': '1605706609',\n",
       " '__zp_sname__': 'a71baa00',\n",
       " '__zp_sseed__': '14P9VO80HWLJiM/naQZkw5A+ZMZZ1weqa6Gk3L/xAGs=',\n",
       " '__zp_sts__': '1605709767637'}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.cookies.get_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "输出某结点在二叉树的中序遍历序列中的后继结点 题目编号:1040\n",
    "题目描述\n",
    "评论\n",
    "\n",
    "二叉树的结点数据域是字符型，空用‘#’表示，键盘输入某结点对应字符，输出该结点在二叉树中序遍历序列中的后继，如果该结点无后继，则输出”none”。\n",
    "已知二叉链表的结点和二叉树类的表示如下：\n",
    "struct BiNode\n",
    "{\n",
    " char data;\n",
    " BiNode *lchild,*rchild;\n",
    "};\n",
    "class BiTree\n",
    "{\n",
    "public:\n",
    " BiTree(){root=Creat(root);}\n",
    "~Bitree() {Release(root);}\n",
    " ......   \n",
    "private:\n",
    " BiNode *root;\n",
    " BiNode* Creat(BiNode *bt);\n",
    " void Release(Binode *bt);\n",
    " ......\n",
    "};\n",
    "BiNode *BiTree::Creat(BiNode *bt)\n",
    "{\n",
    " char ch;\n",
    " cin>>ch;\n",
    " if(ch=='#')\n",
    " return NULL;\n",
    " else\n",
    " {\n",
    "  bt=new BiNode;\n",
    "  bt->data=ch;\n",
    "  bt->lchild=Creat(bt->lchild);\n",
    "  bt->rchild=Creat(bt->rchild);\n",
    " }\n",
    " return bt;\n",
    "}\n",
    "void Bitree::Release(Binode *bt)\n",
    "{\n",
    " if(bt!=NULL)\n",
    " {\n",
    "  Release(bt->lchild);\n",
    "  Release(bt->rchild);\n",
    "  delete bt;\n",
    " }\n",
    "}\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "输入描述\n",
    "第一行输入一棵二叉树的扩展二叉树的前序遍历序列；\n",
    "第二行输入一个结点的数据域（字符）。\n",
    "输出描述\n",
    "输出该结点在二叉树中序遍历序列中的后继，如果该结点无后继，则输出”none”。\n",
    "输入样例\n",
    "AB#C##D##\n",
    "B\n",
    "输出样例\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "s=requests.Session()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
